package com.example.jsoup.kuang.base;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Form Data 类型的数据采用 在地址栏后面拼接 表单请求的数据
 */
public class Detail {

    public static void main(String[] args) throws Exception {

        // 创建 Httpclient 对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /** Form Data 类型的数据采用 在地址栏后面拼接 表单请求的数据 */
        // 创建 httpGet 对象，设置url地址：
        HttpPost httpPost = new HttpPost("https://www.qiyekexie.com/expert/getDetail.do?id=3060849");

//        httpPost.addHeader("accept", "application/json, text/javascript, */*; q=0.01");
//        httpPost.addHeader("accept-encoding", "gzip, deflate, br");
//        httpPost.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
//        httpPost.addHeader("connection", "keep-alive"); ///
//        httpPost.addHeader("content-length", "147"); ///
        httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
//        httpPost.addHeader("cookie", "Hm_lvt_346e80875a7d4528ffaa7b871907c117=1633682839,1633743407,1633913167; acw_tc=2f624a2316339190129448588e24eb894cdc366ca35652b859b5b57854dd3a; JSESSIONID=493F280657A4766031ECFD9B7392E7FA; Hm_lpvt_346e80875a7d4528ffaa7b871907c117=1633919760");
//        httpPost.addHeader("host", "www.qiyekexie.com");
//        httpPost.addHeader("origin","https://www.qiyekexie.com");   ///
//        httpPost.addHeader("referer", "https://www.qiyekexie.com/portal/zkx/6/expert/expertList.action"); ///
//        httpPost.addHeader("sec-fetch-dest", "empty");
//        httpPost.addHeader("sec-fetch-mode", "cors");
//        httpPost.addHeader("sec-fetch-site", "same-site");
        httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.366");

//        String str = "{\"pindex\":2, \"pageSize\":6, \"key\":\"\", \"title\":\"\", \"status\":2, \"provinceCode\":\"\", \"cityCode\":\"\", \"industry\":\"\"}";

//        StringEntity stringEntity = new StringEntity(str, ContentType.APPLICATION_JSON);        //推荐的方法


        // 设置表单的Entity对象到Post请求中
//        httpPost.setEntity(stringEntity);

        CloseableHttpResponse response = null;

        try {
            // 使用httpClient发起请求 获取 response
            response = httpClient.execute(httpPost);

            // 解析响应
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println("\n 请求结果返回hhhhh::::  \n " + content + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                // 关闭httpClient
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                // 关闭response
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
